package com.dy.service.impl;

import com.dy.domain.Lei;
import com.dy.domain.Shangpin;
import com.dy.domain.pojo.PageBean;
import com.dy.mapper.LeiMapper;
import com.dy.service.LeiService;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.util.List;

@Service
@Transactional
public class LeiServieimpl implements LeiService {

    @Autowired
    private LeiMapper leiMapper;

    @Autowired
    private RedisTemplate redisTemplate;






    @Override
    public List<Lei> findall() throws IOException {
        String lei = (String)redisTemplate.boundValueOps("lei").get();

        ObjectMapper objectMapper = new ObjectMapper();

        List<Lei> list = null;

        if (lei != null && lei.length() > 0){

            list = objectMapper.readValue(lei, new TypeReference<List<Lei>>() {});
        }else{

            list = leiMapper.findall();
            redisTemplate.boundValueOps("lei").set(objectMapper.writeValueAsString(list));
        }
        return list;

    }

    @Override
    public PageBean<Lei> findPage(Integer pageNo, Integer pageSize, String ming) {
        PageHelper.startPage(pageNo,pageSize);

        List<Lei> list = leiMapper.mohu(ming);

        PageInfo<Lei> info = new PageInfo<>(list);

        PageBean<Lei> pageBean = new PageBean<>();

        pageBean.setPageCount(info.getPages());
        pageBean.setTotalCount(info.getTotal());
        pageBean.setObject(info.getList());

        return pageBean;
    }
}
